/**
 * 页面加载完毕执行
 */
var contextPath;
function goToPage(pagenum)
{
	$("#userrole").empty();
	$("#userorlepage").empty();
	$.ajax({
		url : contextPath+ 'user/companyuser/userrolelist',
		data : 
		{
			companyRoleId : rid,
			pageszie : 10,
			pagenum : pagenum
		},
		type : 'POST',
		success : function(list) 
		{
			if(list!=null && list.totalRecord>0)
			{
				var join = "";
				for(var i=0;i<list.records.length;i++){
					var usercompany = list.records[i];
					join = join + "<div class='pwsj_right_tjcy_cont'><ul class='tjcy_cont_ul'><li class='number'>"+(i+1)+"</li>" +
							"<li class='username'>"+usercompany.loginName+"</li><li class='tel_num'>"+usercompany.mobile+"</li>" +
							"<li class='operate' style='border-right:none;'>";
					//alert(canadd);
					if(canadd||isSuper)
					{
						join = join + "<a href='javascript:userdel("+rid+","+usercompany.companyUserId+");'>删除</a>";
					}
					join = join + "</li></ul></div>";
				}
				$("#userrole").append(join);
				pageList(list);
			}
		}
	});
}

$(function()
{
	goToPage(1);
});

function goToPage2 (pagenum)
{
	var loginname = $("#loginname").val();
	$("#user").empty();
	$("#userpage").empty();
	
	$.ajax({
		url : contextPath+ 'user/companyuser/userlist',
		data : {
			companyRoleId : rid,
			loginname : loginname,
			pageszie : 6,
			pagenum : pagenum
		},
		type : 'POST',
		success : function(list) 
		{
			//alert(list.records.length);
			if(list!=null && list.totalRecord>0)
			{
				var join = "";
				for(var i=0;i<list.records.length;i++)
				{
					var usercompany = list.records[i];
					join = join + "<div class='add_members_cont'><ul class='add_members_cont_ul'><li class='number'>"+(i+1)+"</li>" +
							"<li class='username'>"+usercompany.loginName+"</li><li class='tel_num'>"+usercompany.mobile+"</li><li class='checked' " +
							"style='position:relative'><input style='position:absolute;left:26px;top:17px;' type='checkbox' id='ckid' name='chkItem' " +
							"value='"+usercompany.companyUserId+"'/></li></ul></div>";
				}
				$("#user").append(join);
				pageList2(list);
			}
		}
	});
	
}

function queryuser()
{
	var name = $("#loginname").val();
	if(name=="")
	{
		alert("请输入查询条件");
		return false;		
	}
	goToPage2(1);
}

function useradd(roleid){
	var str="";
	$("input[name='chkItem']:checkbox").each(function(){ 
        if($(this).attr("checked")){
            str += $(this).val()+",";
        }
    });  
	//alert(str);
	if(str!=""){
		$.ajax({
			url : contextPath+ 'user/companyuser/userroleadd',
			data : {
				roleid : roleid,
				userid : str
			},
			type : 'POST',
			success : function(data) {
				alert(data.msgDesc);
			}
		});
	}else{
		alert("请选择人员");
	}
}

function userdel(roleid,userid){
	if(confirm("确定删除吗")){
		$.ajax({
			url : contextPath+ 'user/companyuser/userroledel',
			data : {
				roleid : roleid,
				userid : userid
			},
			type : 'POST',
			success : function(data) {
				alert(data.msgDesc);
				goToPage(1);
			}
		});
	}
}

function colseuser()
{
	$('.add_members_box').hide(300);
	$('.all_cover').hide(10);
	goToPage(1);
}

function openuser(){
	loaduser();
	$('.all_cover').show(10);
	$('.add_members_box').show(300);
}

function loaduser()
{
	goToPage2(1);
}